package com.ctgu.computer.controller.user;

import com.ctgu.computer.common.FailedMessage;
import com.ctgu.computer.common.SuccessMessage;
import com.ctgu.computer.domain.dto.UserLoginDTO;
import com.ctgu.computer.domain.dto.UserPageDTO;
import com.ctgu.computer.domain.dto.UserSignUpDTO;
import com.ctgu.computer.domain.po.UserInfo;
import com.ctgu.computer.domain.result.PageResult;
import com.ctgu.computer.domain.result.Result;
import com.ctgu.computer.domain.vo.AdminLoginVO;
import com.ctgu.computer.domain.vo.ImagePaths;
import com.ctgu.computer.domain.vo.UserLoginVO;
import com.ctgu.computer.domain.vo.UserSignUpVO;
import com.ctgu.computer.prop.JwtProperties;
import com.ctgu.computer.service.LoginService;
import com.ctgu.computer.service.UserService;
import com.ctgu.computer.utils.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/user")
@Tag(name = "用户相关接口")
public class UserController {

    private final LoginService loginService;
    private final JwtProperties jwtProperties;
    private final UserService userService;

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        UserInfo user = loginService.login(userLoginDTO);
        if(user==null)
            return Result.error(FailedMessage.LOGIN_FAIL);
        if(user.getStatus().equals(0)){
            return Result.error(FailedMessage.FREEZE_USER);
        }
        String jwt = JwtUtils.createJWT(Map.of("userId", user.getUserId()),
                jwtProperties.getSignKey(),
                jwtProperties.getExpireTime());
        return Result.success(SuccessMessage.LOGIN_SUCCESS, new UserLoginVO(jwt));
    }

    @Operation(summary = "用户注册")
    @PostMapping("/signUp")
    public Result signUp(@RequestBody UserSignUpDTO userSignUpDTO) throws IOException {
        UserInfo user = loginService.signUp(userSignUpDTO);
        if(user==null){
            return Result.error(FailedMessage.SIGN_UP_FAIL_USER_ALREADY_EXISTED);
        }
        String jwt = JwtUtils.createJWT(Map.of("userId", user.getUserId()),
                jwtProperties.getSignKey(),
                jwtProperties.getExpireTime());
        return Result.success(UserSignUpVO
                .builder()
                .jwt(jwt)
                .QRCode(user.getQRCode())
                .build());
    }

    @Operation(summary = "获取当前用户信息")
    @GetMapping("/current")
    public Result<UserInfo> getCurrentUserInfo(){
        return Result.success(userService.getInfo());
    }

    @Operation(summary = "根据id获取用户信息")
    @GetMapping("/select/{userId}")
    public Result<UserInfo> getUserInfoById(@PathVariable Integer userId){
        return Result.success(userService.getById(userId));
    }

    @Operation(summary = "这只是一个例子，用来放用户举报二维码页面")
    @GetMapping("/example")
    public void commonPage(String userName, HttpServletResponse response) throws IOException {
        String msg = "This is a judgment page userId:"+userName;
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(response.getOutputStream());
        outputStreamWriter.write(msg);
        outputStreamWriter.close();
    }

    @Operation(summary = "设置用户头像")
    @GetMapping("/image")
    public Result setImage(@RequestParam(name = "file")MultipartFile file){
        userService.addImage(file);
        return Result.success(SuccessMessage.UPDATE_SUCCESS);
    }

}
